package com.bottle.config;

import com.bottle.mode.ReJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: chenyuncong
 * @Date: 2020/4/29 15:31
 */
@ControllerAdvice
@Slf4j
public class MyControllerAdvice {
    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ReJson errorHandler(Exception ex) {
//        ex.printStackTrace();
        log.error("服务器报错",ex);
        ReJson<String> map = new ReJson(500,ex.getMessage(),"服务器出错");
//        ReJson<Exception> map = new ReJson(500,ex,"服务器出错");
        return map;
    }

//    @ResponseStatus(HttpStatus.NOT_MODIFIED)//返回304状态码
    @ResponseBody
    @ExceptionHandler(AsyncRequestTimeoutException.class) //捕获特定异常
    public ReJson handleAsyncRequestTimeoutException(AsyncRequestTimeoutException e, HttpServletRequest request) {
//        log.info("",e);
        ReJson<String> map = new ReJson(304,"","长轮询超时，请重试！");
        return map;
    }

}
